<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends AdminController{
	public $_validation = null;
	function __construct(){
		parent::__construct();
		$this->load->model('MUser');
		$this->_validation = Array();
		$this->pay_status_css = array(
		    '0'=>'label label-sm label-warning',
		    '1'=>'label label-sm label-success',
		    '-1'=>'label label-sm label-default',
		    '-2'=>'label label-sm label-info',
		    '-3'=>'label label-sm label-danger'
		);
		$this->transfer_status_css = array(
		    '0'=>'label label-sm label-danger',
		    '1'=>'label label-sm label-success',
		    '2'=>'label label-sm label-info'
		);
	}

	/**
     *  玩家列表
	 */
	function index(){

		//$this->output->enable_profiler(TRUE);
		$this->load->library('FormFilter');
		$this->load->helper('formfilter');
		
		$this->formfilter->addFilter('id', 'where');
		$this->formfilter->addFilter('account','where');
		$this->formfilter->addFilter('status','where');
		
		$limit = $this->pagination($this->MUser->getCount());
		$list = $this->MUser->getList($limit);

		$this->assign('list', $list);
		$this->layout();
	}

	function edit($id){
		if(((int)$id) . '' !== $id){
			show_error(l('id_or_updated_not_null'));
		}
		if(!$user = $this->MUser->getById($id)){
			errorAndRedirect('此用户账号不存在！', site_url($this->_thisModule.$this->_thisController.'/edit/'.$id));
		}
		$this->load->model('MUserBank');
		$bank_info = $this->MUserBank->getUserBank($id);
		
		//当前用户游戏记录列表
		$this->load->library('FormFilter');
		$this->load->helper('formfilter');
		$this->load->model('MGameRecord');
		$limit = $this->pagination($this->MGameRecord->getCount($id),'',10);
		$gameRecordList = $this->MGameRecord->getList($limit,$id);
		//该用户所有曾经充值订单记录
		$this->load->model('MOrder');
		$orders = $this->MOrder->getAll(Array('user_id'=>$id));
		//该用户所有曾经提款订单记录
		$this->load->model('MWithdraw');
		$withdraws = $this->MWithdraw->getAll(Array('user_id'=>$id));
		//该用户直接上级账号，如果有
		$p_user = $this->MUser->getById($user->p_id);
		//该用户下级列表，如果有
		$childrenUserList = $this->MUser->getChildrenList($id);
		
		$this->assign('user', $user);
		$this->assign('bankInfo', $bank_info);
		$this->assign('list', $gameRecordList);
		$this->assign('orders', $orders);
		$this->assign('withdraws', $withdraws);
		$this->assign('p_user', $p_user);
		$this->assign('childrenUserList', $childrenUserList);
		$this->layout();
	}

	// 修改密码
	function changePass($id, $update_time){
		if(!$this->p->edit){
			//权限不足
			show_error(l('user_has_nopower'));
		}
		if(((int)$id) . '' !== $id){
			show_error(l('id_or_updated_not_null'));
		}
		// 跳转页面
		$redirectUrl = site_url($this->_thisModule.$this->_thisController.'/edit/'.$id);
		if(!$user = $this->MUser->getById((int)$id)){
			model_error('当前用户账号不存在！');
		}
		if ($update_time != $user->update_time) {
			model_error('操作冲突，请重试！');
		}
		$this->load->library('form_validation');
		$this->form_validation->set_rules('password', '密码', 'required');
		$this->form_validation->set_rules('password_confirm', '确认密码', 'required|matches[password]');
		if ($this->form_validation->run()) {
			// 密码修改
			$user->password = IOSS_User::hashPassword($this->input->post('password'));
			if(!$this->MUser->update($user)){
				model_error('数据库更新失败，请联系管理员！');
			}else{
				model_success('修改成功');
			}			
		}
		$this->assign('thisControllerName', '修改密码');
		$this->assign('user', $user);
		$this->layout_modal('change-pass');
	}

	// 改变玩家账户状态
	function toggleStatus($id, $update_time){
		if(!$this->p->edit){
			//权限不足
			show_error(l('user_has_nopower'));
		}
		if(((int)$id) . '' !== $id){
			show_error(l('id_or_updated_not_null'));
		}
		
		// 跳转页面
		$redirectUrl = site_url($this->_thisModule.$this->_thisController.'/edit/'.$id);
		if(!$user = $this->MUser->getById($id)){
			errorAndRedirect('此用户账号不存在！', $redirectUrl);
		}
		if ($update_time != $user->update_time) {
			errorAndRedirect('操作冲突，请重试！', $redirectUrl);
		}

		// 密码修改
		$user->status = !$user->status;
		if(!$this->MUser->update($user)){
			errorAndRedirect('数据库更新失败！', $redirectUrl);
		}
		successAndRedirect('状态修改成功！', $redirectUrl);
	}

	// 删除银行卡
	function deleteBank($user_id){
		if(!$this->p->edit){
			//权限不足
			show_error(l('user_has_nopower'));
		}
		if(!$user = $this->MUser->getById((int)$user_id)){
			show_error('当前用户账号不存在！');
		}		
		$this->load->model('MUserBank');
		if(!$bankInfo = $this->MUserBank->getUserBank($user_id)){
			show_error('当前用户没有有效用户通道！');
		}	

		$this->load->library('form_validation');
		$this->form_validation->set_rules('update_time', '更新时间', 'required');

		if ($this->form_validation->run()) {
			if ($this->input->post('update_time') != $user->update_time) {
				model_error('操作冲突，请重试！');
			}
			if(!$this->MUserBank->deleteBank($bankInfo->id)){
				model_error('数据库删除失败！');
			}else{
				model_success('删除成功');
			}
		}
		$this->assign('thisControllerName', '用户通道信息');
		$this->assign('bankInfo', $bankInfo);
		$this->assign('user', $user);
		$this->layout_modal('user-bank');
	}
}